package fhp.hlhj.giantfold.fragment

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.GridLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.lzy.okgo.model.HttpParams
import fhp.hlhj.giantfold.Contents
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.activity.login.LoginAty
import fhp.hlhj.giantfold.activity.login.WechatLoginAty
import fhp.hlhj.giantfold.activity.malls.MallAty
import fhp.hlhj.giantfold.activity.malls.SearchGoodAty
import fhp.hlhj.giantfold.activity.malls.SuperMarketAty
import fhp.hlhj.giantfold.adapter.FindGoodsAdp
import fhp.hlhj.giantfold.customView.LoadingDialog
import fhp.hlhj.giantfold.customView.TittleGridAdp
import fhp.hlhj.giantfold.interfaces.IAdv
import fhp.hlhj.giantfold.interfaces.ISpendBuy
import fhp.hlhj.giantfold.javaBean.AdvBean
import fhp.hlhj.giantfold.javaBean.TittleBean
import fhp.hlhj.giantfold.network.Urls
import fhp.hlhj.giantfold.presenter.AdvPresenter
import fhp.hlhj.giantfold.presenter.SpendBuyPresenter
import fhp.hlhj.giantfold.utils.LoadingUtils
import fhp.hlhj.giantfold.utils.LogUtil
import fhp.hlhj.giantfold.utils.MyUtils
import kotlinx.android.synthetic.main.fgm_spend_buy.view.*

/**
 * author: FHP
 * data: on 2017\11\22 0022 10:42
 * E-Mail: 103945089@qq.com
 */
class SpendBuyFgm :Fragment(), ISpendBuy,IAdv {
    private lateinit var loadingDialog:LoadingDialog
    private lateinit var rootView:View
    private lateinit var titleAdp:TittleGridAdp
    private lateinit var advPresenter:AdvPresenter
    private lateinit var findGoodsAdp:FindGoodsAdp
    private lateinit var presenter:SpendBuyPresenter
    private  var datas=ArrayList<TittleBean.DataBean>()
    private val findGoodsDatas=java.util.ArrayList<TittleBean.DataBean>()
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        rootView=inflater.inflate(R.layout.fgm_spend_buy,null)
        loadingDialog=LoadingUtils.getInstence(context)
        presenter= SpendBuyPresenter(this)
//        广告
        advPresenter= AdvPresenter(this)
        val advpre = HttpParams()
        advpre.put("block",3)
        advPresenter.getAdv(advpre)
        /*发现好货*/
        findGoodsAdp= FindGoodsAdp(R.layout.find_goods_item,findGoodsDatas)
        rootView.gridView.adapter=findGoodsAdp
        rootView.gridView.layoutManager=GridLayoutManager(context,3)
        val p  = HttpParams()
        p.put("type",3)
        presenter.getFindGoods(p)
        /*tittles*/
        titleAdp=TittleGridAdp(context,datas)
        rootView.tittleGrid.adapter=titleAdp
        val params = HttpParams()
        params.put("type",1)
        presenter.getTittles(params)
        initListener()
        return rootView
    }

    private fun initListener() {
        rootView.etSearch.setOnClickListener{
            if (Contents.userId.equals("0")){
                context.startActivity(Intent(context,WechatLoginAty::class.java))
            }else{
                context.startActivity(Intent(context,SearchGoodAty::class.java))
            }
        }
/*        rootView.btTaoBao.setOnClickListener {
            activity.startActivity(Intent(context,TabBaoAty::class.java))
        }
        rootView.btJingDong.setOnClickListener {
            activity.startActivity(Intent(context, JingDongAty::class.java))
        }
        rootView.btCJHX.setOnClickListener {
            activity.startActivity(Intent(context, SuperMarketAty::class.java))
        }*/
        rootView.tittleGrid.setOnItemClickListener { parent, view, position, id ->
            if (Contents.userId.equals("0")){
                context.startActivity(Intent(context,LoginAty::class.java))
            }else{
                if (datas[position].name.equals("超级惠选")){
                    activity.startActivity(Intent(context, SuperMarketAty::class.java))
                }else{
                    val intent = Intent(context,MallAty::class.java)
                    intent.putExtra("name",datas[position].name)
                    LogUtil.logi("pid为${datas[position].id}")
                    intent.putExtra("pid",datas[position].id)
                    startActivity(intent)
                }
            }


        }
    }


    override fun showLoading() {
        loadingDialog.show()
    }

    override fun getFirst(tittleBean: TittleBean) {
            loadingDialog.dismiss()
            datas.clear()
            datas.addAll(tittleBean.data)
            titleAdp.notifyDataSetChanged()
    }

    override fun getFindGoods(tittleBean: TittleBean) {
        loadingDialog.dismiss()
        findGoodsDatas.clear()
        findGoodsDatas.addAll(tittleBean.data)
        findGoodsAdp.notifyDataSetChanged()
    }

    override fun getADV(advBean: AdvBean) {
        if (advBean.code==200){
            advBean.data.forEachIndexed {
                i, dataBean ->
                when(i){
                    0->{
                        Glide.with(context).load(Urls.BaseUrl+dataBean.adv_img).asBitmap().dontAnimate().placeholder(R.mipmap.exp2).into(rootView.btAdv1)
                        if (dataBean.jump_type.equals("1")){
                            rootView.btAdv1.setOnClickListener {
                                val intent = Intent()
//Intent intent = new Intent(Intent.ACTION_VIEW,uri);
                                intent.setAction("android.intent.action.VIEW")
                                val content_url = Uri.parse(dataBean.jump_url)
                                intent.setData(content_url)
                                context.startActivity(intent)
                            }
                        }
                    }
                    1->{
                        Glide.with(context).load(Urls.BaseUrl+dataBean.adv_img).asBitmap().dontAnimate().placeholder(R.mipmap.exp2).into(rootView.btAdv2)
                        if (dataBean.jump_type.equals("1")){
                            rootView.setOnClickListener {
                                val intent = Intent()
//Intent intent = new Intent(Intent.ACTION_VIEW,uri);
                                intent.setAction("android.intent.action.VIEW")
                                val content_url = Uri.parse(dataBean.jump_url)
                                intent.setData(content_url)
                                context.startActivity(intent)
                            }
                        }
                    }
                    2->{
                        Glide.with(context).load(Urls.BaseUrl+dataBean.adv_img).asBitmap().dontAnimate().placeholder(R.mipmap.exp2).into(rootView.ig_1)
                        rootView.title_1.text=dataBean.adv_name
                        rootView.info_1.text=dataBean.adv_title
                        if (dataBean.jump_type.equals("1")){
                            rootView.ig_1.setOnClickListener {
                                val intent = Intent()
//Intent intent = new Intent(Intent.ACTION_VIEW,uri);
                                intent.setAction("android.intent.action.VIEW")
                                val content_url = Uri.parse(dataBean.jump_url)
                                intent.setData(content_url)
                                context.startActivity(intent)
                            }
                        }
                    }
                    3->{
                        Glide.with(context).load(Urls.BaseUrl+dataBean.adv_img).asBitmap().dontAnimate().placeholder(R.mipmap.exp2).into(rootView.ig_2)
                        rootView.title_2.text=dataBean.adv_name
                        rootView.info_2.text=dataBean.adv_title
                        if (dataBean.jump_type.equals("1")){
                            rootView.ig_2.setOnClickListener {
                                val intent = Intent()
//Intent intent = new Intent(Intent.ACTION_VIEW,uri);
                                intent.setAction("android.intent.action.VIEW")
                                val content_url = Uri.parse(dataBean.jump_url)
                                intent.setData(content_url)
                                context.startActivity(intent)
                            }
                        }
                    }
                    4->{
                        Glide.with(context).load(Urls.BaseUrl+dataBean.adv_img).asBitmap().dontAnimate().placeholder(R.mipmap.exp2).into(rootView.ig_3)
                        rootView.title_3.text=dataBean.adv_name
                        rootView.info_3.text=dataBean.adv_title
                        if (dataBean.jump_type.equals("1")){
                            rootView.ig_2.setOnClickListener {
                                val intent = Intent()
//Intent intent = new Intent(Intent.ACTION_VIEW,uri);
                                intent.setAction("android.intent.action.VIEW")
                                val content_url = Uri.parse(dataBean.jump_url)
                                intent.setData(content_url)
                                context.startActivity(intent)
                            }
                        }
                    }
                    5->{
                        Glide.with(context).load(Urls.BaseUrl+dataBean.adv_img).asBitmap().dontAnimate().placeholder(R.mipmap.exp2).into(rootView.ig_4)
                        rootView.title_4.text=dataBean.adv_name
                        rootView.info_4.text=dataBean.adv_title
                        if (dataBean.jump_type.equals("1")){
                            rootView.ig_4.setOnClickListener {
                                val intent = Intent()
//Intent intent = new Intent(Intent.ACTION_VIEW,uri);
                                intent.setAction("android.intent.action.VIEW")
                                val content_url = Uri.parse(dataBean.jump_url)
                                intent.setData(content_url)
                                context.startActivity(intent)
                            }
                        }
                    }
                    6->{
                        Glide.with(context).load(Urls.BaseUrl+dataBean.adv_img).asBitmap().dontAnimate().placeholder(R.mipmap.exp2).into(rootView.ig_5)
                        rootView.title_5.text=dataBean.adv_name
                        rootView.info_5.text=dataBean.adv_title
                        if (dataBean.jump_type.equals("1")){
                            rootView.ig_5.setOnClickListener {
                                val intent = Intent()
//Intent intent = new Intent(Intent.ACTION_VIEW,uri);
                                intent.setAction("android.intent.action.VIEW")
                                val content_url = Uri.parse(dataBean.jump_url)
                                intent.setData(content_url)
                                context.startActivity(intent)
                            }
                        }
                    }
                    7->{
                        Glide.with(context).load(Urls.BaseUrl+dataBean.adv_img).asBitmap().dontAnimate().placeholder(R.mipmap.exp2).into(rootView.btAdv3)
                        if (dataBean.jump_type.equals("1")){
                            rootView.btAdv3.setOnClickListener {
                                val intent = Intent()
//Intent intent = new Intent(Intent.ACTION_VIEW,uri);
                                intent.setAction("android.intent.action.VIEW")
                                val content_url = Uri.parse(dataBean.jump_url)
                                intent.setData(content_url)
                                context.startActivity(intent)
                            }
                        }
                    }
                }
            }
        }
    }
}